# coding: utf-8
from abc import ABC
from typing import List, Union

import requests
from PySide6.QtGui import QIcon
from bs4 import BeautifulSoup
from qfluentwidgets import FluentIcon, FluentIconBase

from .api_models import HomeItemModel, BookInfoModel, SearchResultModel, ChapterPhotoModel
from ..settings import USER_AGENT

requests.packages.urllib3.disable_warnings()


class APIBase(ABC):
    name = 'APIBase'  # 插件名称，子类必须定义
    description = ''  # 插件描述，子类必须定义
    link = ''  # 网站链接
    local_file = ''  # 本地文件路径
    icon: Union[str, FluentIconBase, QIcon] = FluentIcon.APPLICATION  # 插件图标
    version = '1.0.0'  # 插件版本
    author = 'author'  # 插件作者

    headers = {'User-Agent': USER_AGENT}

    @classmethod
    def send(cls, method: str, url: str, *args, **kwargs):
        """
        发送请求
        :param method: 请求方法
        :param url: 请求地址
        :param args: 请求参数
        :param kwargs: 请求参数
        :return:
        """
        if 'headers' not in kwargs:
            kwargs['headers'] = cls.headers
        if 'timeout' not in kwargs:
            kwargs['timeout'] = 10
        response = requests.request(method, url, *args, verify=False, **kwargs)
        response.raise_for_status()
        return response

    @classmethod
    def get(cls, url: str, *args, **kwargs):
        return cls.send('GET', url, *args, **kwargs)

    @classmethod
    def post(cls, url: str, *args, **kwargs):
        return cls.send('POST', url, *args, **kwargs)

    @classmethod
    def put(cls, url: str, *args, **kwargs):
        return cls.send('PUT', url, *args, **kwargs)

    @classmethod
    def delete(cls, url: str, *args, **kwargs):
        return cls.send('DELETE', url, *args, **kwargs)

    @classmethod
    def patch(cls, url: str, *args, **kwargs):
        return cls.send('PATCH', url, *args, **kwargs)

    @classmethod
    def head(cls, url: str, *args, **kwargs):
        return cls.send('HEAD', url, *args, **kwargs)

    @classmethod
    def options(cls, url: str, *args, **kwargs):
        return cls.send('OPTIONS', url, *args, **kwargs)

    @classmethod
    def url_to_bs4(cls, url: str, *args, **kwargs) -> BeautifulSoup:
        response = cls.get(url, headers=cls.headers, *args, **kwargs)
        return BeautifulSoup(response.text, 'lxml')

    @classmethod
    def home(cls) -> List[HomeItemModel]:
        """
        漫画首页
        :return:
        """
        raise NotImplementedError

    @classmethod
    def info(cls, *args, **kwargs) -> BookInfoModel:
        """
        漫画信息
        :return:
        """
        raise NotImplementedError

    @classmethod
    def search(cls, *args, **kwargs) -> SearchResultModel:
        """
        搜索漫画
        :param args:
        :param kwargs:
        :return:
        """
        raise NotImplementedError

    @classmethod
    def photos(cls, *args, **kwargs) -> ChapterPhotoModel:
        """
        漫画图片
        :param args:
        :param kwargs:
        :return:
        """
        raise NotImplementedError
